package com.rcw.filelib.ui;

import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rcw.filelib.FileType;
import com.rcw.filelib.R;
import com.rcw.filelib.adapter.FileListAdapter;
import com.rcw.filelib.bean.Document;

import java.util.ArrayList;
import java.util.List;


public class DocFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";

    private FileType fileType;

    private OnFragmentInteractionListener mListener;

    public DocFragment() {
    }

    public static DocFragment newInstance(FileType param1) {
        DocFragment fragment = new DocFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            fileType = getArguments().getParcelable(ARG_PARAM1);
        }
    }

    public FileType getFileType() {
        return fileType;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_doc, container, false);
        initView(view);
        return view;
    }

    RecyclerView recyclerView;
    TextView emptyView;
    FileListAdapter fileListAdapter;

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        emptyView = (TextView) view.findViewById(R.id.empty_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setVisibility(View.GONE);

    }

    public void onSelectDoc(Document doc) {
        if (mListener != null) {
            mListener.onFragmentInteraction(doc);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Document doc);
    }

    List<Document> dirs = new ArrayList<>();

    public void updateList(List<Document> docs) {
        if (getView() == null)
            return;

        if (docs.size() > 0) {
            dirs.clear();
            dirs.addAll(docs);
            recyclerView.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            fileListAdapter = (FileListAdapter) recyclerView.getAdapter();
            if (fileListAdapter == null) {
                fileListAdapter = new FileListAdapter(getActivity(), R.layout.item_file_list,dirs);
                recyclerView.setAdapter(fileListAdapter);
            } else {
                fileListAdapter.setData(dirs);
                fileListAdapter.notifyDataSetChanged();
            }
            fileListAdapter.setOnItemClickListener(new FileListAdapter.OnItemClickListener() {
                @Override
                public void setOnClickListener(View view, int position) {
                    onSelectDoc(dirs.get(position));
                }

                @Override
                public void setOnLongClickListener(View view, int position) {

                }
            });
        } else {
            recyclerView.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }
}
